package com.hsytech.dijiebao.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by 46812 on 2015/8/17.
 */
public class ImageUtil {

    private static String imageUri = "/soft2t/images/";
    private static final String TAG = "compressimage";

    /**
     * 质量压缩方法
     *
     * @param image
     * @return
     */
    public static byte[] compressImage(Bitmap image, int fileSize) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //璐ㄩ噺鍘嬬缉鏂规硶锛岃繖閲�100琛ㄧず涓嶅帇缂╋紝鎶婂帇缂╁悗鐨勬暟鎹瓨鏀惧埌baos涓�

        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        int options = 100; //姝ゅ鍙皾璇曠敤90%寮�濮嬪帇缂╋紝璺宠繃100%鍘嬬缉
        // 寰幆鍒ゆ柇濡傛灉鍘嬬缉鍚庡浘鐗囨槸鍚﹀ぇ浜�100kb,澶т簬缁х画鍘嬬缉
        int length = baos.toByteArray().length;
        Log.i(TAG, options + "--" + length);
        while ((length = baos.toByteArray().length) / 1024 > fileSize) {
            // 姣忔閮藉噺灏�10
            options -= 10;
            // 閲嶇疆baos鍗虫竻绌篵aos
            baos.reset();
            // 杩欓噷鍘嬬缉options%锛屾妸鍘嬬缉鍚庣殑鏁版嵁瀛樻斁鍒癰aos涓�
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);
            Log.i(TAG, options + "--" + length);
        }
        return baos.toByteArray();
        // 鎶婂帇缂╁悗鐨勬暟鎹産aos瀛樻斁鍒癇yteArrayInputStream涓�
//		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        // 鎶夿yteArrayInputStream鏁版嵁鐢熸垚鍥剧墖
//		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
//		return bitmap;
    }

    /**
     * 图片按比例大小压缩方法（根据路径获取图片并压缩）
     *
     * @param srcPath
     * @return
     */
    public static byte[] getImage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //寮�濮嬭鍏ュ浘鐗囷紝姝ゆ椂鎶妎ptions.inJustDecodeBounds 璁惧洖true浜�
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//姝ゆ椂杩斿洖bm涓虹┖

        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        //鐜板湪涓绘祦鎵嬫満姣旇緝澶氭槸800*480鍒嗚鲸鐜囷紝鎵�浠ラ珮鍜屽鎴戜滑璁剧疆涓�
        float hh = 800f;//杩欓噷璁剧疆楂樺害涓�800f
        float ww = 480f;//杩欓噷璁剧疆瀹藉害涓�480f
        //缂╂斁姣斻�傜敱浜庢槸鍥哄畾姣斾緥缂╂斁锛屽彧鐢ㄩ珮鎴栬�呭鍏朵腑涓�涓暟鎹繘琛岃绠楀嵆鍙�
        int be = 1;//be=1琛ㄧず涓嶇缉鏀�
        if (w > h && w > ww) {//濡傛灉瀹藉害澶х殑璇濇牴鎹搴﹀浐瀹氬ぇ灏忕缉鏀�
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//濡傛灉楂樺害楂樼殑璇濇牴鎹搴﹀浐瀹氬ぇ灏忕缉鏀�
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
//        鍙﹀涓�绉嶇畻鍘嬬缉姣旂殑鏂规硶锛�
//        鍥剧墖姣斾緥鍘嬬缉鍊嶆暟 灏辨槸 锛堝搴﹀帇缂╁�嶆暟+楂樺害鍘嬬缉鍊嶆暟锛�/2
//        be = (int) ((w / STANDARD_WIDTH + h/ STANDARD_HEIGHT) / 2);
        newOpts.inSampleSize = be;//璁剧疆缂╂斁姣斾緥
        //閲嶆柊璇诲叆鍥剧墖锛屾敞鎰忔鏃跺凡缁忔妸options.inJustDecodeBounds 璁惧洖false浜�
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return compressImage(bitmap, 100);//鍘嬬缉濂芥瘮渚嬪ぇ灏忓悗鍐嶈繘琛岃川閲忓帇缂�
    }

    /**
     * * 图片按比例大小压缩方法（根据Bitmap图片压缩）
     *
     * @param image
     * @return
     */
    public static Bitmap comp(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        if (baos.toByteArray().length / 1024 > 1024) {//鍒ゆ柇濡傛灉鍥剧墖澶т簬1M,杩涜鍘嬬缉閬垮厤鍦ㄧ敓鎴愬浘鐗囷紙BitmapFactory.decodeStream锛夋椂婧㈠嚭
            baos.reset();//閲嶇疆baos鍗虫竻绌篵aos
            image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//杩欓噷鍘嬬缉50%锛屾妸鍘嬬缉鍚庣殑鏁版嵁瀛樻斁鍒癰aos涓�
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //寮�濮嬭鍏ュ浘鐗囷紝姝ゆ椂鎶妎ptions.inJustDecodeBounds 璁惧洖true浜�
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        //鐜板湪涓绘祦鎵嬫満姣旇緝澶氭槸800*480鍒嗚鲸鐜囷紝鎵�浠ラ珮鍜屽鎴戜滑璁剧疆涓�
        float hh = 800f;//杩欓噷璁剧疆楂樺害涓�800f
        float ww = 480f;//杩欓噷璁剧疆瀹藉害涓�480f
        //缂╂斁姣斻�傜敱浜庢槸鍥哄畾姣斾緥缂╂斁锛屽彧鐢ㄩ珮鎴栬�呭鍏朵腑涓�涓暟鎹繘琛岃绠楀嵆鍙�
        int be = 1;//be=1琛ㄧず涓嶇缉鏀�
        if (w > h && w > ww) {//濡傛灉瀹藉害澶х殑璇濇牴鎹搴﹀浐瀹氬ぇ灏忕缉鏀�
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {//濡傛灉楂樺害楂樼殑璇濇牴鎹搴﹀浐瀹氬ぇ灏忕缉鏀�
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0)
            be = 1;
        newOpts.inSampleSize = be;//璁剧疆缂╂斁姣斾緥
        //閲嶆柊璇诲叆鍥剧墖锛屾敞鎰忔鏃跺凡缁忔妸options.inJustDecodeBounds 璁惧洖false浜�
        isBm = new ByteArrayInputStream(baos.toByteArray());
        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        byte[] b = compressImage(bitmap, 100);//鍘嬬缉濂芥瘮渚嬪ぇ灏忓悗鍐嶈繘琛岃川閲忓帇缂�
        ByteArrayInputStream in = new ByteArrayInputStream(b);
        return BitmapFactory.decodeStream(in, null, null);
    }


    public static String getCompressedPath(String  filePath){
//        String destPath = getImageCachePath();

        byte[] bytes =getImage(filePath);
        String destPath = getImageCachePath()  + bytes.hashCode() + ".jpg";

//        File f = new File(destPath);
//        if (!f.exists()){
//            f.mkdirs();
//        }



        InputStream in = new ByteArrayInputStream(bytes);
        try {
            OutputStream out = new FileOutputStream(destPath);
            int length = 0;
            byte[] b = new byte[1024];
            while((length=in.read(b))>0) {
                out.write(b, 0, length);
            }
            out.flush();
            out.close();
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  destPath;
    }


    public static String getImageCachePath(){

        String imageCachePath = "";
        if(FileUtil.isSDCardEnable()){
            imageCachePath = FileUtil.getSDCardPath_M1();
        }else{
            imageCachePath = FileUtil.getRootDirectoryPath();
        }

        return imageCachePath + imageUri;
    }



}
